#pragma once
#include <iostream>
#include <cstdio>
#include <cstdarg>
#include <string>
#include <cstring>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

// 设计一个错误信息的文件

//using namespace std;

#define SIZE 1024

// Log等级
#define Info 0
#define Waring 1
#define Error 2
#define Fatel 3
#define Debug 4

// 输出方式
#define Screen 1
#define OneFile 2
#define ClassFile 3

#define FileName "log.txt"

class log
{

public:
    log(int method = Screen)
        : _printmethod(method)
    {
        mkdir(_path.c_str(),0775);
    }
    log(int method, std::string path)
        : _printmethod(method), _path(path)
    {
        mkdir(_path.c_str(),0775);
    }

    void Enable(int method)
    {
        if(method==Screen||method==OneFile||method==ClassFile)
            _printmethod = method;
        else
            std::cout<<"There is no such method"<<std::endl;   
    }

    // template<typename...Args>
    // 为什么不能用C++的函数包方式实现?可以不可以用函数包实现？
    void operator()(int level, const char *format, ...)
    {
        // 这一块是获取时间的知识
        time_t nowtime = time(nullptr);
        auto ctime = localtime(&nowtime);

        // 这一块涉及snprintf的用法
        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d.%d]", getlevel(level).c_str(), ctime->tm_year + 1900,
                 ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        // 这一块涉及C的可变参数知识及vsnsprintf
        va_list ap;
        va_start(ap, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, ap);
        va_end(ap);

        // char logtxt[SIZE*2];
        // snprintf(logtxt,sizeof(logtxt),leftbuffer,rightbuffer);
        std::string logtxt = leftbuffer;
        logtxt += rightbuffer;
        logtxt +="\n";

        logprint(logtxt, level);
    }
    ~log() {}

private:
    void logprint(const std::string &logtxt, int level)
    {
        std::string path = "./log/";
        path += FileName;
        switch (_printmethod)
        {
        case (Screen):
            std::cout << logtxt << std::endl;
            break;
        case (OneFile):
            printOneFile(logtxt,path);
            break;
        case (ClassFile):
            printClassFile(logtxt,level,path);
            break;
        }
    }

    void printOneFile(const std::string &logtxt,const std::string &path)
    {
        int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    void printClassFile(const std::string &logtxt, int level,std::string& path)
    {
        //cout<<3<<endl;
        std::string newpath=_path;
        newpath+=getlevel(level);
        newpath+='.';
        newpath+=FileName;
        printOneFile(logtxt,newpath);
    }

    std::string getlevel(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
        case Waring:
            return "Waring";
        case Error:
            return "Error";
        case Fatel:
            return "Fatel";
        case Debug:
            return "Debug";
        default:
            return "None";
        }
    }

private:
    int _printmethod;
    std::string _path="./log/";
};
